<?php

class Setup_DictionaryController extends Mac_Controller {

    public function init() {
        //$this->layout->addScript('jquery.js');
        $this->layout->addStylesheet('styles.css');
        $this->layout->setHeadTitle('Crosser Page Test');
    }

    public function indexAction() {
        $cpt = 0;
        Database_Mysql::dbSelect('dictionary');
        Database_Mysql::setDebugMode(false);
        $lang = 1;
        
        $start = microtime(true);
        
        $words = file(ROOT_PATH . '/export/liste-mots-tagges.txt');
        foreach ($words as $word) {
            $cpt++;
            $split = array_reverse(explode("\t", $word));
            $mot = trim(array_pop($split));
            if (mb_strlen(trim($mot)) > 1) {
                $mot_exist = Database_Mysql::connect()->existsID('word', 'content', $mot);
                if (!$mot_exist) {
                    $idmot = Database_Mysql::connect()->insert('word', array('content' => $mot));
                } else {
                    $idmot = Database_Mysql::connect()->getById('word', 'idword', 'content', $mot);
                }
                foreach ($split as $type_lemme) {
                    list($type, $lemme) = explode(' ', $type_lemme);
                    $type = trim($type);
                    $detail_type = explode(':', $type);
                    list($type, $form) = (count($detail_type) == 2) ? $detail_type : array($type, null);
                    $lemme = trim($lemme);
                    
                    if ($lemme != '<unknown>') {
                        $lemme_exist = Database_Mysql::connect()->existsID('lemme', 'content', $lemme);
                        if (!$lemme_exist) {
                            $idlemme = Database_Mysql::connect()->insert('lemme', array('content' => $lemme, 'idlanguage' => $lang));
                        } else {
                            $idlemme = Database_Mysql::connect()->getById('lemme', 'idlemme', 'content', $lemme);
                        }
                        $asso_exists = Database_Mysql::connect()->exists('word_lemme', array(
                            'eq' => array(
                                'idword' => $idmot,
                                'idlemme' => $idlemme
                            )
                        ));
                        if (!$asso_exists) {
                            Database_Mysql::connect()->insert('word_lemme', array('idword' => $idmot, 'idlemme' => $idlemme, 'type' => $type, 'form' => $form));
                        }
                    }
                }
            }
        }
        
        $end = microtime(true);
        echo 'Time : '.(($end-$start)/1000);
        die;
        
        $mot = Database_Mysql::connect('dictionary')->getById('lemme', 'content', 'idlemme', 1);
        die($mot);
    }
}
